//
// Created by zhuzonghui on 2022/12/18.
//
#include "../../header/service/SubjectService.h"
#include <mysql.h>
#include "../../header/tools/MYSQL_TOOLS.h"

Subject *getSubject(MYSQL_ROW column) {
    Subject *subject = new Subject;
    subject->setSubjectId(*column[0] - 48);
    subject->setSubjectName(column[1]);
    return subject;
}

list<Subject *> *wzy_get_subject_list() {
    string sql = "select * from subject";
    list<Subject *> *subjectList = new list<Subject *>;
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        MYSQL_ROW column;
        while ((column = mysql_fetch_row(pRes))) {
            subjectList->push_back(getSubject(column));
        }
        return subjectList;
    } catch (...) {
        return subjectList;
    }
}

void wzy_add_subject(char *subjectName) {
    string sql = "select * from subject where subject_name='" + (string) subjectName + "'";
    MYSQL_RES *pRes = wzy_mysql_query(sql);
    my_ulonglong i = pRes->row_count;
    if (i > 0) {
        throw -1;
    }
    sql = "insert into subject(subject_name) values('" + (string) subjectName + "')";
    try {
        wzy_mysql_query(sql);
    } catch (...) {
        throw -2;
    }
}

